class Person implements Serializable{
  
  String userName
  String password
  String regNo
  String firstname
  String lastname
  String email
  Role role
  
  static hasMany = [comments:Comment, reports:Report, supervisedProjects:Project]
  static mappedBy = [supervisedProjects:"supervisor"]

  static constraints = {
    
    userName(blank:false)
    password(blank:false, password:true)
    regNo(blank:false)
    firstname(blank:false)
    lastname(blank:false)
    email(email:true)
    role(nullable:true)

  }  

    String toString() {
    "$firstname $lastname"
  }

	static mapping = {
		columns {
			role column: 'role'
		}
	}

	static def supervisorList() {
		def role = Role.find("from Role as r where r.name='Supervisor'")
		def organiserRole = Role.find("from Role as r where r.name='Organiser'")
		return Person.findAll("from Person as p where p.role = $role.id or p.role = $organiserRole.id order by p.firstname, p.lastname")
	}

	def chosenProject() {
		return Project.findWhere(student: Person.get(this.id))
	}
}
